package com.ruoyi.web.controller.grade;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.grade.domain.entity.MsUser;
import com.ruoyi.grade.service.IMsUserService;
import com.ruoyi.grade.service.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@CrossOrigin
@Api(tags = "学生账号模块")
@RestController
@RequestMapping("/student")
@PreAuthorize("@ss.hasPermi('student:controller')")
public class StudentController extends BaseController {

    @Autowired
    private IStudentService studentAccountService;
    @Autowired
    private IMsUserService userService;


    @ApiOperation(value = "导入学生账号密码(返回插入失败信息)")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPermi('student:account:import')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "认证码", paramType = "header", required = true),
            @ApiImplicitParam(name = "updateSupport", value = "是否更新支持，如果已存在，则进行更新数据", required = true)
    })
    public AjaxResult importStudent(@RequestPart MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<MsUser> util = new ExcelUtil<MsUser>(MsUser.class);
        List<MsUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = userService.importUser(userList, updateSupport, operName);
        return success(message);
    }

}
